/*
 * Created on 2004-4-28
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package jaoso.framework.test;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;

import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 */
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import java.util.Iterator;

/**
 * DOCUMENT ME!
 * 
 * @author $author$
 * @version $Revision: 1.1 $
 */
public class Xmltest
{
    /**
     * DOCUMENT ME!
     * 
     * @param doc
     *            DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     */
    public static Element getRootElement(Document doc)
    {
        return doc.getRootElement();
    }

    /**
     * DOCUMENT ME!
     * 
     * @param args
     *            DOCUMENT ME!
     */
    public static void main(String[] args)
    {
        String path = Xmltest.class.getResource("/acl-config.xml").getPath();
        System.out.println(path);
        path = path.substring(1, path.length());
        System.out.println(path);

        Document doc = read(path);
        System.out.println(doc.asXML());

        Element root = getRootElement(doc);
        Element item;
        Element node;

        for (Iterator i = root.elementIterator(); i.hasNext();)
        {
            item = (Element) i.next();
            System.out.println(item.asXML());

            for (Iterator ii = item.elementIterator(); ii.hasNext();)
            {
                node = (Element) ii.next();
                System.out.println(node.getName());
                System.out.println(node.getText());
            }

            //end for
        }

        //end for
        item = root.addElement("protected-resource");
        item.addElement("box").addText("102");
        item.addElement("desc").addText("the three page");
        item.addElement("url").addText("three.do");

        //write(doc, path);
    }

    /**
     * DOCUMENT ME!
     * 
     * @param fileName
     *            DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     */
    public static Document read(String fileName)
    {
        SAXReader reader = new SAXReader();
        Document document = null;

        try
        {
            document = reader.read(fileName);
        } catch (DocumentException e)
        {
            System.out.println(e.getMessage());
        }

        return document;
    }

    /**
     * DOCUMENT ME!
     * 
     * @param doc
     *            DOCUMENT ME!
     * @param path
     *            DOCUMENT ME!
     */
    public static void write(Document doc, String path)
    {
        /*
         * try{ FileWriter out = new FileWriter(path); doc.write(out); } catch
         * (IOException e){ System.out.println(e.getMessage()); }
         */
        try
        {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter output = new XMLWriter(new FileWriter(new File(path)),
                    format);
            output.write(doc);
            output.close();
        } catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
}